﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TradeMeSDK;

namespace TradeMo
{
	public partial class CriteriaUsedCarsView : IBackButtonController
	{
		private readonly SelectorHelper<Locality> _regionSelector = new SelectorHelper<Locality>();
		private readonly SelectorHelper<string> _selector = new SelectorHelper<string>();
		private readonly CriteriaUsedCarsViewModel _viewModel;

		public CriteriaUsedCarsView(CriteriaUsedCarsViewModel criteria)
		{
			InitializeComponent();
			_viewModel = criteria;
			DataContext = _viewModel;
			VisualStateManager.GoToState(this, "HiddenState", false);

			LayoutUpdated += (e, s) => VisualStateManager.GoToState(this, "VisibleState", true);
		}

		public bool AnySelectorOpen
		{
			get
			{
				return _regionSelector.IsOverlayOpen ||
					   _selector.IsOverlayOpen;
			}
		}

		public Action RefreshAction { get; set; }

		private void RegionGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_regionSelector.Items = _viewModel.Regions;
			_regionSelector.OpenPopup(LayoutRoot, () =>
			{
				SelectorItem<Locality> selected = _regionSelector.Selected;
				if (selected == null) return;

				_viewModel.SelectedRegion = selected.Item;
			});

		}

		private void SortByGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_selector.Items = _viewModel.SortByItems;
			_selector.OpenPopup(LayoutRoot, () =>
			{
				var selected = _selector.Selected;
				if (selected == null) return;

				_viewModel.SelectedSortBy = selected;
			});
		}

		private void BodyStyleGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_selector.Items = _viewModel.BodyStyleItems;
			_selector.OpenPopup(LayoutRoot, () =>
			{
				var selected = _selector.Selected;
				if (selected == null) return;

				_viewModel.SelectedBodyStyle = selected;
			});
		}

		private void MakeGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_selector.Items = _viewModel.MakeItems;
			_selector.OpenPopup(LayoutRoot, () =>
			{
				var selected = _selector.Selected;
				if (selected == null) return;

				_viewModel.SelectedMake = selected;
			});
		}

		private void PriceFrom_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_selector.Items = _viewModel.PriceItems;
			_selector.OpenPopup(LayoutRoot, () =>
			{
				var selected = _selector.Selected;
				if (selected == null) return;

				_viewModel.SelectedPriceFrom = selected;
			});
		}

		private void PriceTo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_selector.Items = _viewModel.PriceItems;
			_selector.OpenPopup(LayoutRoot, () =>
			{
				var selected = _selector.Selected;
				if (selected == null) return;

				_viewModel.SelectedPriceTo = selected;
			});
		}

		private void YearFrom_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_selector.Items = _viewModel.YearItems;
			_selector.OpenPopup(LayoutRoot, () =>
			{
				var selected = _selector.Selected;
				if (selected == null) return;

				_viewModel.SelectedYearFrom = selected;
			});
		}

		private void YearTo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			_selector.Items = _viewModel.YearItems;
			_selector.OpenPopup(LayoutRoot, () =>
			{
				var selected = _selector.Selected;
				if (selected == null) return;

				_viewModel.SelectedYearTo = selected;
			});
		}

		public bool CanClose()
		{
			return !AnySelectorOpen;
		}

		public void Close(Action onClosed)
		{
			VisualStateManager.GoToState(this, "HiddenVisualState", true);
			if (onClosed != null)
				TradeMeDispatcher.OnUIThread(300, onClosed);
		}

		public void CloseChild()
		{
			if (_regionSelector.IsOverlayOpen) _regionSelector.ClosePopup();
			if (_selector.IsOverlayOpen) _selector.ClosePopup();
		}

		private void RefreshButton_Click(object sender, RoutedEventArgs e)
		{
			if (RefreshAction != null)
				RefreshAction();
		}

        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.Reset();
        }



	}
}
